﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FreeDAM.Domain.Abstract;
using FreeDAM.Domain.Entities;
using System.Data.Entity;

namespace FreeDAM.WebUI.Areas.Admin.Controllers
{
    public class TagsController : Controller
    {
        private readonly ITagRepository TagRepository;
        private readonly ITagCategoryRepository TagCategoryRepository;

        public TagsController(ITagRepository tagRepository, ITagCategoryRepository tagCategoryRepository)
        {
            TagRepository = tagRepository;
            TagCategoryRepository = tagCategoryRepository;
        }

        public ActionResult Index()
        {
            return View(TagRepository.All);
        }

        public ActionResult Create()
        {
            ViewBag.TagCategories = new SelectList(TagCategoryRepository.All, "Id", "Name");

            return View();
        }

        [HttpPost]
        public ActionResult Create(Tag viewModel)
        {
            ActionResult result = View(viewModel);

            if (ModelState.IsValid)
            {
                TagRepository.InsertOrUpdate(viewModel);
                TagRepository.Save();
                result = RedirectToAction("Index");
            }

            return result;
        }

        public ActionResult Edit(int id)
        {
            ViewBag.TagCategories = new SelectList(TagCategoryRepository.All, "Id", "Name");
            return View(TagRepository.Find(id));
        }

        [HttpPost]
        public ActionResult Edit(Tag viewModel)
        {
            ActionResult result = View(viewModel);

            if (ModelState.IsValid)
            {
                TagRepository.InsertOrUpdate(viewModel);
                TagRepository.Save();
                result = RedirectToAction("Index");
            }

            return result;
        }
    }
}
